//整个项目的所有接口，便于统一管理，节省代码
//起名字要以API结尾，为了和别的方法区分开
//不允许被覆盖，用const方法
//每个方法都对应一个后台接口，哪些变量会变

//axios的全局配置；
axios.defaults.baseURL = "http://123.57.109.30:3007";
//axios响应拦截器
//axios发起请求以后，响应回来，触发then方法之前，先被响应器拦截，响应一边，then在执行

//请求拦截器
axios.interceptors.request.use(function(config) { //在发送请求之前做一些事情return config;   
    if(!config.url.startsWith("/api")){
        config.headers['Authorization'] = sessionStorage.getItem("token");
    }
    return config;
},function(error) { //做一些有请求错误的事情
    return Promise .reject(error );   
});


//添加响应拦截器
axios.interceptors.response.use(function(response) { //使用响应数据返回响应；  当返回http状态2或3开头执行
    let layer = layui.layer;
    let {status,message} = response.data;
    layer.msg(message);//弹窗
    if(status == 0) {
        //登录成功，进入到index页面
        // window.location.href = "./index.html"
        return response;
    }else {
        return Promise.reject(message);
    }  
},function(error) { //使用响应错误返回Promise .reject（error） ;  当http状态4或5执行
    let {response:{data:{message}}} = error;//返回error中的messsage信息，提示给用户
    layer.msg(message,{
        time:1500,//弹出消息框1.5秒
        end:function() {//end方法，弹消失时触发的代码
            sessionStorage.removeItem("token");//过期把token清空
            window.parent.location.href = "/login.html"//过期后返回登录界面
            //  /代表当前网页的根目录
        }
    });
    return Promise.reject(error);
});

//登录
const loginAPI = (argStr,fn) => {
    axios({
        method :"post",
        url : '/api/login',
        data : argStr
    }).then(res => {
        //弹窗
        // let layer = layui.layer;
        // let {status,message} = res.data;
        // if(status == 0) {
        //     //登录成功，进入到index页面
        //     // window.location.href = "./index.html"
        //     fn();
        // }
        // layer.msg(message);
        fn(res);
    })
}

//注册
const registerAPI = (argStr,fn) => {
    axios({
        method :"post",
        url : '/api/reguser',
        data : argStr
    }).then(res => {
        //弹窗
        // let layer = layui.layer;
        // let {status,message} = res.data;
        // layer.msg(message);
        // if(status == 0){//逻辑上完全正确，注册成功
        //     //注册成功，注册页面隐藏
        //     //$("#register").hide();
        //     fn();
        // }
        fn(res);//res相当于响应拦截器的response（后台返回的对象）
    })
}

//个人中心
const getUinforAPI = (argStr,fn) => {
    axios({
        method :"GET",
        url : '/my/userinfo'
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}

//个人中心，更新用户信息
const upDataInfoAPI = (argStr,fn) =>{
    axios({
        method :"POST",
        url : '/my/userinfo',
        data :argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}


//修改密码
const changePassAPI = (argStr,fn) =>{
    axios({
        method :"POST",
        url : '/my/updatepwd',
        data :argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}


//修改头像
const upHeadImgAPI = (argStr,fn) => {
    axios({
        method :"POST",
        url : '/my/update/avatar',
        data :argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}



//分类-新增文章分类
const addCateAPI = (argStr,fn) => {
    axios({
        method :"POST",
        url : '/my/article/addcates',
        data :argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}

//分类-获取列表
const acateListAPI = (argStr,fn) => {
    axios({
        method :"GET",
        url : '/my/article/cates',
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}


//分类-删除列表
const delCateAPI = (id,fn) => {
    axios({
        method :"GET",
        url : '/my/article/deletecate/' + id,
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}


//分类-编辑列表
const updateCateAPI = (argStr,fn) => {
    axios({
        method :"POST",
        url : '/my/article/updatecate',
        data:argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}



//文章-发布新文章
const addArtAPI = (argStr,fn) => {
    axios({
        method :"POST",
        url : '/my/article/add',
        data:argStr
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}


//文章-获取文章列表数据
const artListAPI = (argStr,fn) => {
    axios({
        method :"GET",
        url : '/my/article/list',
        params:argStr//get方式要用params方式传参，params会自动把对象转成字符串
        //post方式要用data方式，需要自己将对象转成字符串
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}

//文章-删除
const delArtAPI = (id,fn) => {
    axios({
        method :"GET",
        url : '/my/article/delete/' + id,
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}

//查询
const detailArtAPI = (id,fn) => {
    axios({
        method :"GET",
        url : '/my/article/' + id,
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}

//文章更新
const updataArtAPI = (argStr,fn) => {
    axios({
        method :"POST",
        url : '/my/article/edit',
        data: argStr,
        //headers:{'Authorization':sessionStorage.getItem("token")}
    }).then(res => {
        fn(res);
    })
}